import {showTime} from "../../function/dateFunc";

export namespace StDate {
    export type TypeInitDate = string | number | Date;
    export type TypeTimeUnit = "year" | "month" | "day" | "hour" | "second" |
        "minute" | "millisecond" | "monthIndex" | "timestamp" | "dayInWeek";
}
export class StDate {
    public readonly dateObj: Date;
    constructor(date: StDate.TypeInitDate | StDate = Date.now()) {
        if (date instanceof StDate) {
            return date;
        }
        if (date instanceof Date) {
            this.dateObj = date;
        } else {
            this.dateObj = new Date(date);
        }
    }
    public isValid(): boolean {
        const num = this.getTimestamp();
        return !isNaN(num);
    }
    public getDateInstance() {
        return new Date(this.getTimestamp());
    }
    public getByUnit(unit: StDate.TypeTimeUnit) {
        switch (unit) {
            case "year":
                return this.getYear();
            case "month":
                return this.getMonth();
            case "monthIndex":
                return this.getMonthIndex();
            case "day":
                return this.getDay();
            case "hour":
                return this.getHour();
            case "minute":
                return this.getMinute();
            case "second":
                return this.getSecond();
            case "millisecond":
                return this.getMillisecond();
            case "timestamp":
                return this.getTimestamp();
            case "dayInWeek":
                return this.getDayInWeek();
        }
    }
    public getYear() {
        return this.dateObj.getFullYear();
    }
    public getMonth() {
        return this.getMonthIndex() + 1;
    }
    public getMonthIndex() {
        return this.dateObj.getMonth();
    }
    public getDay() {
        return this.dateObj.getDate();
    }
    public getHour() {
        return this.dateObj.getHours();
    }
    public getMinute() {
        return this.dateObj.getMinutes();
    }
    public getSecond() {
        return this.dateObj.getSeconds();
    }
    public getMillisecond() {
        return this.dateObj.getMilliseconds();
    }
    public getTimestamp() {
        return this.dateObj.getTime();
    }
    public getDayInWeek() {
        return this.dateObj.getDay();
    }
    public format(formatType: string = "YYYY-MM-DD HH:mm:ss"): string {
        return showTime(this.getTimestamp(), formatType);
    }
    public getMinuteStart() {
        const str = this.format("YYYY-MM-DD HH:mm:00");
        return new StDate(str);
    }
    public getHourStart() {
        const str = this.format("YYYY-MM-DD HH:00:00");
        return new StDate(str);
    }
    public getDayStart() {
        const str = this.format("YYYY-MM-DD 00:00:00");
        return new StDate(str);
    }
    public getMonthStart() {
        const str = this.format("YYYY-MM-01 00:00:00");
        return new StDate(str);
    }
    public getYearStart() {
        const str = this.format("YYYY-01-01 00:00:00");
        return new StDate(str);
    }
    public setYear(year: number) {
        const str = this.format(year + "-MM-DD HH:mm:ss");
        return new StDate(str);
    }
}
